
/*
 *  
 *
 *  Copyright (C) Microsoft Corporation.  All rights reserved
 *
 *  
 */

using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Globalization;


namespace CRMScenarios
{
    /// <summary>
    /// Some common utilities
    /// </summary>
    static class Util
    {
        /// <summary>
        /// Creates an XmlDocument object with secure default property values.
        /// </summary>
        /// <param name="reader">The XmlTextReader to get the data from.</param>
        /// <returns>the new XmlDocument object</returns>
        public static XmlDocument CreateXmlDocument(XmlTextReader reader)
        {
            reader.ProhibitDtd = true;
            reader.XmlResolver = null;
            reader.WhitespaceHandling = WhitespaceHandling.Significant;

            XmlDocument xmlDoc = new XmlDocument();

            // Set XmlResolver to null to prevent usage of the default XmlResolver.
            xmlDoc.XmlResolver = null;
            xmlDoc.Load(reader);

            reader.Close();

            return xmlDoc;
        }

        /// <summary>
        /// Creates an XmlDocument object with secure default property values.
        /// Loads the given XML into the XmlDocument.
        /// </summary>
        /// <param name="xml">The XML to load.</param>
        /// <returns>the new XmlDocument object</returns>
        public static XmlDocument CreateXmlDocument(string xml)
        {
            //If no xml to load, create an empty XmlDocument.
            if (xml == null || xml.Length == 0)
            {
                XmlDocument xmlDoc = new XmlDocument();

                // Set XmlResolver to null to prevent usage of the default XmlResolver.
                xmlDoc.XmlResolver = null;
                return xmlDoc;
            }

            return CreateXmlDocument(new XmlTextReader(new StringReader(xml)));
        }

        /// <summary>
        /// Show an error message
        /// </summary>
        /// <param name="err"></param>
        public static void ShowError(string err)
        {
            if ((err == null) || (err.Length == 0))
            {
                return;
            }
            System.Windows.Forms.MessageBox.Show(err, global::CRMScenarios.Resources.DialogBoxTitle,
                                                 System.Windows.Forms.MessageBoxButtons.OK, 
                                                 System.Windows.Forms.MessageBoxIcon.Error);
        }

        /// <summary>
        /// Show an message
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowMsg(string msg)
        {
            if ((msg == null) || (msg.Length == 0))
            {
                return;
            }
            System.Windows.Forms.MessageBox.Show(msg, global::CRMScenarios.Resources.DialogBoxTitle,
                                                 System.Windows.Forms.MessageBoxButtons.OK, 
                                                 System.Windows.Forms.MessageBoxIcon.Information);
        }

        /// <summary>
        /// Ask a  Ok/Cancel question. 
        /// </summary>
        /// <param name="ques"></param>
        /// <returns></returns>
        public static System.Windows.Forms.DialogResult AskQues(string ques)
        {
            if ((ques == null) || (ques.Length == 0))
            {
                return System.Windows.Forms.DialogResult.Cancel;
            }

            return System.Windows.Forms.MessageBox.Show(ques, 
                                                 global::CRMScenarios.Resources.DialogBoxTitle,
                                                 System.Windows.Forms.MessageBoxButtons.OKCancel,
                                                 System.Windows.Forms.MessageBoxIcon.Question);
        }
    }
    /// <summary>
    /// The class implements the back and forth logic
    /// </summary>
    public class BackForthInfo
    {
        private StringCollection RecordTypes = null;
        private Guid[] RecordIds = null;
        private int CurrentPointer = 0;

        public BackForthInfo()
        {
            RecordTypes = new StringCollection();
            RecordIds = new Guid[10]; // TODO
            CurrentPointer = 0;
        }

        /// <summary>
        /// Add the information of new record to the back and forth class
        /// </summary>
        /// <param name="RecordType"></param>
        /// <param name="RecordId"></param>
        public void NewRecordEntry(string RecordType, Guid RecordId)
        {
            if ((RecordType == null) || (RecordId == Guid.Empty))
            {
                return;
            }

            if (RecordTypes.Count == 0)
            {
                RecordTypes = new StringCollection();
                RecordIds = new Guid[10]; // TODO
            }

            if (RecordTypes.Count == 10) // TODO
            {
                RecordTypes.RemoveAt(0);
                int i = 0;
                for (i = 0; i < RecordTypes.Count; i++)
                {
                    RecordIds[i] = RecordIds[i + 1];
                }
            }
            RecordTypes.Add(RecordType);
            RecordIds[RecordTypes.Count - 1] = RecordId;
            CurrentPointer = RecordTypes.Count - 1;
        }

        /// <summary>
        /// Get the record type of the current entty
        /// </summary>
        /// <returns></returns>
        public string RecordType()
        {
            if ((RecordTypes == null) || (RecordTypes.Count == 0) || (CurrentPointer < 0) || (CurrentPointer >= RecordTypes.Count))
            {
                return null;
            }    
            return RecordTypes[CurrentPointer];
        }

        /// <summary>
        /// Get the guid of the current entty
        /// </summary>
        /// <returns></returns>
        public Guid RecordId()
        {
            if ((RecordIds == null) || (RecordIds.Length == 0) || (CurrentPointer < 0) || (CurrentPointer >= RecordTypes.Count))
            {
                return Guid.Empty;
            }
            return RecordIds[CurrentPointer];
        }

        /// <summary>
        /// 
        /// </summary>
        public void MoveForward()
        {
            if (CurrentPointer < (RecordTypes.Count - 1)) 
            {
                CurrentPointer++;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void MoveBackvard()
        {
            if (CurrentPointer > 0)
            {
                CurrentPointer--;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool CanGoFroward()
        {
            if ((RecordTypes == null) || (RecordTypes.Count == 0)) 
            {
                return false;
            }
            return (CurrentPointer < (RecordTypes.Count - 1)); // TODO - read from config;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool CanGoBackward()
        {
            return (CurrentPointer > 0);
        }
    }

    /// <summary>
    /// Class includes the details of PageInfo.
    /// </summary>
    public class PageInfo
    {
        private PagingInfo pInfo = null;

        private int _MaxRecCountInGrid;
        public int MaxRecCountInGrid
        {
            get
            {
                return _MaxRecCountInGrid;
            }
        }

        public PageInfo()
        {
            pInfo = new PagingInfo();
            _MaxRecCountInGrid = ConfigFileManager.MaxRecCountInGrid(25);
        }

        /// <summary>
        /// Gives info about first page
        /// </summary>
        /// <returns></returns>
        public PagingInfo FirstPage()
        {
            if (pInfo == null)
            {
                pInfo = new PagingInfo();
            }
            pInfo.Count = MaxRecCountInGrid;
            pInfo.PageNumber = 1;
            return pInfo;
        }

        /// <summary>
        /// Gives info about next page
        /// </summary>
        /// <returns></returns>
        public PagingInfo NextPage()
        {
            if (pInfo == null)
            {
                return FirstPage();
            }
            pInfo.PageNumber += 1;
            return pInfo;
        }

        /// <summary>
        /// Gives info about previous page
        /// </summary>
        /// <returns></returns>
        public PagingInfo PrevPage()
        {
            if (pInfo == null)
            {
                return FirstPage();
            }
            if (pInfo.PageNumber > 1)
            {
                pInfo.PageNumber -= 1;
            }
            return pInfo;
        }

        /// <summary>
        /// Gives the page number
        /// </summary>
        /// <returns></returns>
        public int CurrentPage()
        {
            if (pInfo != null)
            {
                return pInfo.PageNumber;
            }
            return 0;
        }
    }

    /// <summary>
    /// A class not used.
    /// </summary>
    public class ListViewItemSorter : System.Collections.IComparer 
    {
        private SortOrder order;
        private int sortColumn;
        CultureInfo cultureInfo;

        public ListViewItemSorter(SortOrder theOrder, int col, string _cultureName)
        {
            order = theOrder;
            sortColumn = col;
            cultureInfo = new CultureInfo(_cultureName);
        }

        public int Compare(object x, object y)
        {
            int result = 0;
            result = String.Compare(
                ((ListViewItem)x).SubItems[sortColumn].Text,
                ((ListViewItem)y).SubItems[sortColumn].Text,
                true,
                cultureInfo
                );

            if (order != SortOrder.Ascending)
            {
                result = 0 - result;
            }
            return (result);
        }
    }
}
